
package de.godlike.android.web;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class KsoapWrapper {

    private static final String SOAP_ACTION = "";

    private static final String NAMESPACE = "http://services.godlike.de/";

    private String serverUrl;

    private HttpTransportSE httpConn;

    public KsoapWrapper(String serverUrl) {
        this.setServerUrl(serverUrl);
    }

    public String getServerUrl() {
        return this.serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
        this.httpConn = new HttpTransportSE(this.serverUrl);
    }

    /**
     * @param methodName - Name der Methode
     * @param attributes - Array mit den Parameter der Methode
     * @return
     */
    public SoapSerializationEnvelope callMethod(String methodName, String attributes[][]) {
        SoapObject request = new SoapObject(KsoapWrapper.NAMESPACE, methodName);
        // Request.addProperty("", "");

        for (String[] tmp : attributes) {
            request.addAttribute(tmp[0], tmp[1]);
        }

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        try {
            httpConn.call(KsoapWrapper.SOAP_ACTION, envelope);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return envelope;
    }

    /**
     * @param methodName - Name der Methode
     * @return
     */
    public SoapSerializationEnvelope callMethod(String methodName) {
        SoapObject request = new SoapObject(KsoapWrapper.NAMESPACE, methodName);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        try {
            httpConn.call(KsoapWrapper.SOAP_ACTION, envelope);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return envelope;
    }
}
